package com.ujiuye.web;


import com.ujiuye.entity.UserInfo;
import com.ujiuye.service.UserService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取页面表单数据
        Map<String, String[]> map = req.getParameterMap();
        //通过beanUtils对象映射-->表单name属性值和对象属性保持一致
        UserInfo u = new UserInfo();
        try {
            BeanUtils.populate(u,map);
            System.out.println(u);
            UserService us = new UserService();
            boolean flag = us.addUser(u);
            if(flag){
                //注册成功-->跳转登录页面
                req.getRequestDispatcher("login.html").forward(req,resp);
            }else{
                //注册失败
                req.getRequestDispatcher("register.html").forward(req,resp);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
